package com.yun.baymax.custom;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yun.baymax.R;

import java.util.ArrayList;

public class PasswordDialog extends AppCompatDialogFragment {
    public static final String DIALOG_TYPE = "type";
    public static int type;
    ArrayList<String> data = new ArrayList<>();
    String one = "0";
    String two = "0";
    String three = "0";
    String four = "0";
    OnClickListener onClickListener;
    OnClickListener onResetListener;
    private TextView tvTitle;
    private Button open;
    private ImageView reset;

    public static final PasswordDialog newInstance(int type) {
        PasswordDialog dialog = new PasswordDialog();
        Bundle bundle = new Bundle(1);
        bundle.putInt(DIALOG_TYPE, type);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt(DIALOG_TYPE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.password_dialog, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        EasyPickerView ep1 = (EasyPickerView) view.findViewById(R.id.epv1);
        EasyPickerView ep2 = (EasyPickerView) view.findViewById(R.id.epv2);
        EasyPickerView ep3 = (EasyPickerView) view.findViewById(R.id.epv3);
        EasyPickerView ep4 = (EasyPickerView) view.findViewById(R.id.epv4);
        tvTitle = (TextView) view.findViewById(R.id.title);
        reset = (ImageView) view.findViewById(R.id.reset);
        tvTitle.setText(type == 0 ? "设置密码" : "新姿势解锁");
        open = (Button) view.findViewById(R.id.open);
        open.setText(type == 0 ? "确定" : "解锁");
        for (int i = 0; i < 10; i++) {
            data.add(String.valueOf(i));
        }
        ep1.setDataList(data);
        ep2.setDataList(data);
        ep3.setDataList(data);
        ep4.setDataList(data);

        ep1.setOnScrollChangedListener(new EasyPickerView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int curIndex) {

            }

            @Override
            public void onScrollFinished(int curIndex) {
                one = data.get(curIndex);
            }
        });
        ep2.setOnScrollChangedListener(new EasyPickerView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int curIndex) {

            }

            @Override
            public void onScrollFinished(int curIndex) {
                two = data.get(curIndex);
            }
        });
        ep3.setOnScrollChangedListener(new EasyPickerView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int curIndex) {

            }

            @Override
            public void onScrollFinished(int curIndex) {
                three = data.get(curIndex);
            }
        });
        ep4.setOnScrollChangedListener(new EasyPickerView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int curIndex) {

            }

            @Override
            public void onScrollFinished(int curIndex) {
                four = data.get(curIndex);
            }
        });

        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null)
                    onClickListener.onClick(one + two + three + four, type);
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    dismiss();
                    onResetListener.onClick("", type);
                }
            }
        });
    }

    public void setOnClickListener(OnClickListener onItemClickListener) {
        this.onClickListener = onItemClickListener;
    }

    public void setOnResetListener(OnClickListener onResetListener) {
        this.onResetListener = onResetListener;
    }
}
